﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using Database.Classes;
using System.Collections.Generic;


public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void UpdateCulture()
    {
        int intervalTime = int.Parse(GetConfigurationForOption("SmallUpdateInterval"));//in seconds
        double CultureBonus = double.Parse(GetConfigurationForOption("CultureBonus"));

        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(@"SELECT     SUM(dbo.Planets.Cybernets) AS Expr1, dbo.Players.PlayerId, dbo.Players.Culture, CurrentCulture, ArtifactID
FROM         dbo.Planets INNER JOIN
                      dbo.Players ON dbo.Planets.OwnerID = dbo.Players.PlayerId
GROUP BY dbo.Players.PlayerId, dbo.Players.Culture, CurrentCulture, ArtifactID
HAVING      (dbo.Players.PlayerId > 1)", conn))
            {
                SqlDataReader rdr = cmd.ExecuteReader();
                Dictionary<string, double> commands = new Dictionary<string, double>();
                while (rdr.Read())
                {
                    int culture = int.Parse(rdr["Culture"].ToString());
                    int pId = int.Parse(rdr["PlayerID"].ToString());
                    double  sciH = (int)double.Parse(rdr["Expr1"].ToString());
                    double cLv = double.Parse(rdr["CurrentCulture"].ToString());
                    int levelPoints = Equations.CultureCost((int)cLv);
                    //get bonus from race and artifact
                    double bonus = (1 + culture * CultureBonus);
                    string t = rdr["ArtifactID"] as string;
                    if (t != null)
                    {
                        switch (t)
                        {
                            case "GT": bonus += 0.1; break;
                            case "ITV": bonus += 0.2; break;
                            case "BHS": bonus -= 0.1; break;
                            case "STr": bonus += 0.1; break;
                            case "AF": bonus -= 0.3; break;
                            case "MOT": bonus += 0.2; break;
                        }
                    }
                    sciH = sciH * bonus;
                    if (sciH < 0)
                        sciH = 0.01;
                    //get percentage
                    double currentValue = (1.0 - (cLv - (int)cLv)) * levelPoints * 1.0;
                    double newValue = currentValue * 1.0 - sciH / (double)(3600.0 / intervalTime);
                    double valueToStore;
                    if (newValue <= 0) //new level
                    {
                        valueToStore = 1.0 - (Equations.CultureCost((int)cLv + 1) + newValue) * 1.0 / Equations.CultureCost((int)cLv + 1) + (int)cLv + 1;
                    }
                    else
                    {
                        valueToStore = 1.0 - (newValue) / levelPoints + 1.0 * (int)cLv;
                    }
                    //store in the db
                    commands.Add(StoreCulture(pId), valueToStore);
                }
                rdr.Close();
                cmd.Parameters.Add("@val", SqlDbType.Float);
                foreach (string s in commands.Keys)
                {
                    cmd.CommandText = s;
                    cmd.Parameters["@val"].Value = commands[s];
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }

    public static string StoreCulture(int pId)
    {
        return "Update Players Set CurrentCulture = @val Where PlayerID = " + pId.ToString();
    }
};
